﻿using SelfManagement.Common.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SelfManagement.Utility.Extensions;

namespace SelfManagement.Mvc.Result
{
    public static class JsonResultDataExtensions
    {
        public static ILogger Logger { set; private get; }

        public static JsonResultData RunWithTry(this JsonResultData jsonResultEntry, Action<JsonResultData> runMethod, ILogger logger = null)
        {
            try
            {
                runMethod.Invoke(jsonResultEntry);
            }
            catch (Exception ex)
            {
                jsonResultEntry.Success = false;
                //jsonResultEntry.Messages.Clear();
                jsonResultEntry.AddMessage(ex.Message);
                if (logger.IsNull())
                {
                    logger = Logger;
                }
                if(Logger.IsNotNull())
                {
                    Logger.LogException($"RunWithTry Error, {System.Web.HttpContext.Current.Request.Url.AbsolutePath}", ex);
                }
            }
            return jsonResultEntry;
        }
    }
}
